Imports System.IO

Public Enum LogMode
    File
    Web
    EventViewer
    MessageBox
End Enum

Public Class SysLog
    Private Const sLogFileExt As String = ".Log"
    Protected sLogFileSpec As String = ""
    Protected bUseEventLog As Boolean = False
    Protected bUseWebLog As Boolean = False
    Protected bUseFileLog As Boolean = True
    Protected bUseMessageBox As Boolean = False
    Public Event ShowMessage(ByVal sMessage As String)

#Region " Properties "

    Public Property FileSpec() As String
        Get
            Return sLogFileSpec
        End Get
        Set(ByVal value As String)
            sLogFileSpec = value
        End Set
    End Property

    Public ReadOnly Property WebTrace() As System.Web.TraceContext
        Get
            Return System.Web.HttpContext.Current.Trace
        End Get
    End Property

#End Region


#Region " Methods "

    Public Sub New()
        'Creates a log file called "[Application].Log"
        'If they use this method, it appends.
        Dim sAppName As String = GenApp.ApplicationName
        Dim sAppDir As String = GenApp.ApplicationHomeDirectory
        FileSpec = sAppDir + sAppName + sLogFileExt
        Start(True)
    End Sub

    Public Sub New(ByVal bAppend As Boolean)
        Dim sAppName As String = GenApp.ApplicationName
        Dim sAppDir As String = GenApp.ApplicationHomeDirectory
        FileSpec = sAppDir + sAppName + sLogFileExt
        Start(bAppend)
    End Sub

    Public Sub New(ByVal sFileSpec As String)
        'By Default, append to the file.
        Me.New(sFileSpec, True)
    End Sub

    Public Sub New(ByVal sFileSpec As String, ByVal bAppend As Boolean)
        FileSpec = sFileSpec
        Start(bAppend)
    End Sub

    Public Sub SetLogMode(ByVal aLogMode As LogMode, ByVal bSwitch As Boolean)
        Select Case aLogMode
            Case Is = LogMode.EventViewer
                bUseEventLog = bSwitch
            Case Is = LogMode.File
                bUseFileLog = bSwitch
            Case Is = LogMode.Web
                bUseWebLog = bSwitch
            Case Is = LogMode.MessageBox
                bUseMessageBox = bSwitch
        End Select
    End Sub

    Public Sub Delete()
        If System.IO.File.Exists(FileSpec) Then
            System.IO.File.Delete(FileSpec)
        End If
    End Sub

    Public Sub Start(ByVal bAppend As Boolean, ByVal sVersion As String)
        If InStr(FileSpec, "\") = 0 Then
            If InStr(FileSpec, "/") = 0 Then
                'Append the path to the program.
                FileSpec = GenApp.ApplicationHomeDirectory + FileSpec
            End If
        End If
        If Not bAppend Then
            Delete()
            LogText("-------------------- Start Logging on Application ----------------------")
            If sVersion <> "" Then
                LogText("Version: " + sVersion)
            End If
        End If
    End Sub

    Public Sub Start(ByVal bAppend As Boolean)
        If InStr(FileSpec, "\") = 0 Then
            If InStr(FileSpec, "/") = 0 Then
                'Append the path to the program.
                FileSpec = GenApp.ApplicationHomeDirectory + FileSpec
            End If
        End If
        If Not bAppend Then
            Delete()
            LogText("-------------------- Start Logging on Application ----------------------")
        End If
    End Sub

    Public Sub StopLog()
        LogText("+++++++++++++++++++ Logging Stopped on Application +++++++++++++++++++++")
    End Sub

    Public Sub View()
        Try
            Dim sStartProg As String
            sStartProg = "notepad.exe"
            System.Diagnostics.Process.Start(sStartProg, sLogFileSpec)
        Catch ex As Exception
            LogText("Error - " & ex.Message)
        End Try
    End Sub

    Public Sub LogText(ByVal s As String)

        If bUseEventLog Then
            Dim el As EventLog = New EventLog
            el.Source = GenApp.ApplicationName
            el.WriteEntry(s)
        End If

        If bUseFileLog Then
            Try
                Dim fs As FileStream = New FileStream(FileSpec, FileMode.OpenOrCreate, FileAccess.ReadWrite, System.IO.FileShare.ReadWrite)
                Dim w As StreamWriter = New StreamWriter(fs)
                w.BaseStream.Seek(0, SeekOrigin.End)
                w.Write(DateTime.Now.ToString("MM/dd/yyyy hh:mm:sstt"))
                w.Write(": ")
                w.Write(s)
                w.Write(Environment.NewLine)
                w.Flush()
                w.Close()
            Catch ex As Exception
                'The file was already open by another process, can not log it.
            End Try
        End If

        If bUseWebLog Then
            If GenApp.isWebApp Then
                WebTrace.Warn(s)
            End If
        End If

        If bUseMessageBox Then
            If Not GenApp.isWebApp Then
                System.Windows.Forms.MessageBox.Show(s)
            Else
                'Need code here to get the message back to the page controls and add it to them.
                'The calling page has to register with this event to get the message.
                'Done in the base class.
                RaiseEvent ShowMessage(s)
            End If
        End If
    End Sub

    Public Function GetLogText(ByVal bHtmlFormat As Boolean) As String
        'Load the log text into a string
        Dim s As String
        s = SysFile.GetFileText(FileSpec)
        If Not bHtmlFormat Then
            Return s
        End If
        s = s.Replace(Environment.NewLine, "<br>")
        Return s
    End Function

#End Region


End Class
